package com.easylinkin.linkappapi.progress.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.progress.entity.ProgressImage;
import com.easylinkin.linkappapi.progress.entity.ProgressImageSet;
import com.easylinkin.linkappapi.progress.entity.vo.ProgressImageSetVo;
import com.easylinkin.linkappapi.progress.mapper.ProgressImageMapper;
import com.easylinkin.linkappapi.progress.mapper.ProgressImageSetMapper;
import com.easylinkin.linkappapi.progress.service.ProgressImageSetService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 形象进度-统计设置
 * </p>
 *
 * @author kanyuanfeng
 * @since 2023-04-13
 */
@Service
@Slf4j
public class ProgressImageSetServiceImpl extends ServiceImpl<ProgressImageSetMapper, ProgressImageSet> implements ProgressImageSetService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;
  @Autowired
  private ProgressImageMapper progressImageMapper;

  /**
   * 增加
   * @param progressImageSets
   */
  @Override
  public void insertList(List<ProgressImageSet> progressImageSets) {
    progressImageSets.forEach(progressImageSet->{
      if (null != progressImageSet.getId()){
        this.saveOrUpdate(progressImageSet,(t) -> updateOne(t));
      }else {
        this.saveOrUpdate(progressImageSet,(t) -> save(t));
      }
    });
  }

  @Override
  public List<ProgressImageSetVo> queryList() {
    String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
    LambdaQueryWrapper<ProgressImageSet> queryWrapper = new LambdaQueryWrapper();
    queryWrapper.eq(ProgressImageSet::getTenantId,tenantId);
    List<ProgressImageSet> list = baseMapper.selectList(queryWrapper);
    List<ProgressImageSetVo> progressImageSetVos = ProgressImageSetVo.getprogressImageList();
    progressImageSetVos.forEach(p->{
      list.stream().filter(l->p.getCode().equals(l.getCode())).findFirst().ifPresent(o->
        BeanUtil.copyProperties(o,p,"name")
      );
    });
    return progressImageSetVos;
  }

  /**
   * 执行新增和更新
   * @param progressImageSet
   * @param consumer 自定义执行
   */
  private void saveOrUpdate(ProgressImageSet progressImageSet, Consumer<ProgressImageSet> consumer) {
    /**
     * 验证重复
     */
    //设置基本属性
    this.setBase(progressImageSet);
    consumer.accept(progressImageSet);
  }

  /**
   * 修改
   */
  private void updateOne(ProgressImageSet progressImageSet) {
    //查询旧的数据
    ProgressImageSet oldProgressImageSet = this.getById(progressImageSet.getId());
    if (!progressImageSet.getComputeType().equals(progressImageSet.getComputeType())){
      //两种计算方式不一样,清除之前的数据
      LambdaQueryWrapper<ProgressImage> deleteWrapper = new LambdaQueryWrapper<>();
      deleteWrapper.eq(ProgressImage::getTenantId,oldProgressImageSet.getTenantId())
                      .eq(ProgressImage::getCode,oldProgressImageSet.getCode());
      progressImageMapper.delete(deleteWrapper);
    }
    this.updateById(progressImageSet);
  }

  /**
   * 设置基本属性
   * @param progressImageSet
   */
  private void setBase(ProgressImageSet progressImageSet) {
    Long userId = null;
    if(null != linkappUserContextProducer.getCurrent()){
      userId = linkappUserContextProducer.getCurrent().getId();
    }
    progressImageSet.setModifyTime(new Date());
    progressImageSet.setModifyId(userId);
    //没有id就是新增,有就是编辑
    if (null == progressImageSet.getId()){
        if (StringUtils.isBlank(progressImageSet.getTenantId())){
          progressImageSet.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        }
      progressImageSet.setCreatorId(userId);
      progressImageSet.setCreateTime(new Date());
    }
  }
}
